package cn.bonoon.kernel.web.html;

import javax.servlet.ServletContext;

import cn.bonoon.kernel.util.StringHelper;

/**
 * <pre>
 * 用于描述一个将要被生成的html界面的信息
 * </pre>
 * @author jackson
 *
 */
public class InternalHtmlInfo {
	/**
	 * 网站servlet的上下文
	 */
	private final ServletContext servletContext;

	/**
	 * 生成的html代码主体部分
	 */
	private final StringBuilder html;
	
	/**
	 * <pre>
	 * 将被放在
	 * &lt;script&gt;
	 *     $(function(){
	 *         //block code
	 *     });
	 * &lt;/script&gt;
	 * 里的js代码
	 * </pre>
	 */
	private final StringBuilder block;

	/**
	 * 直接放在&lt;script&gt;标签里的js代码
	 */
	private final StringBuilder script;
	
	/**
	 * 被引入的js或css
	 */
	private final StringBuilder includes;
	
	public InternalHtmlInfo(ServletContext servletContext){
		this.html = new StringBuilder();
		this.block = new StringBuilder();
		this.script = new StringBuilder();
		this.includes = new StringBuilder();
		this.servletContext = servletContext;
	}
	
	public String path(String url){
		if(url.startsWith("/")){
			return StringHelper.path(servletContext.getContextPath(), url);
		}
		return url;
	}
	
	public StringBuilder append(String str){
		return html.append(str);
	}

	public StringBuilder append(char c) {
		return html.append(c);
	}

	public StringBuilder append(int i){
		return html.append(i);
	}

	public StringBuilder append(StringBuilder sb) {
		return html.append(sb);
	}

	public boolean isEmpty() {
		return html.length() == 0;
	}

	public StringBuilder getHtml() {
		return html;
	}

	public StringBuilder getBlock() {
		return block;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public StringBuilder getScript() {
		return script;
	}

	public StringBuilder getIncludes() {
		return includes;
	}

	public StringBuilder block(String block) {
		return this.block.append(block);
	}

	public StringBuilder script(String script) {
		return this.script.append(script);
	}

	public StringBuilder includes(String includes) {
		return this.includes.append(includes);
	}
	
}
